package com.vooda.business_admin.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vooda.business_admin.common.ReqDataTableDto;
import com.vooda.business_admin.common.RspDataTableDto;
import com.vooda.business_admin.dao.TicketMapper;
import com.vooda.business_admin.dao.TicketTypeMapper;
import com.vooda.business_admin.entity.Ticket;
import com.vooda.business_admin.service.MarketService;
import com.vooda.business_admin.service.StoreService;
import com.vooda.business_admin.service.TicketService;
import com.vooda.business_admin.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true)
public class TicketServiceImpl implements TicketService{

    @Autowired
    private TicketMapper ticketMapper;

    @Autowired
    private StoreService storeService;

    @Autowired
    private MarketService marketService;

    @Autowired
    private TicketTypeMapper ticketTypeMapper;

    @Override
    @Transactional(readOnly = false)
    public String deleteByPrimaryKey(Long id) {
        Ticket ticket = selectByPrimaryKey(id);
        ticket.setIsDel(true);
        return updateByPrimaryKeySelective(ticket);
    }

    @Override
    @Transactional(readOnly = false)
    public String insertSelective(Ticket record) {
        Long maxId = ticketMapper.findMaxId();
        record.setId(maxId+1);
        record.setCreateTime(new Date());
        record.setIsDel(false);
        record.setModifiedTime(new Date());
        return ticketMapper.insertSelective(record)>0?"success":"fail";
    }

    @Override
    public Ticket selectByPrimaryKey(Long id) {
        return ticketMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = false)
    public String updateByPrimaryKeySelective(Ticket record) {
        record.setModifiedTime(new Date());
        if(record.getIsForever()){
            Ticket ticket = selectByPrimaryKey(record.getId());
            record.setStartTime(null);
            record.setEndTime(null);
            record.setCreateTime(ticket.getCreateTime());
            record.setIsHappyBag(ticket.getIsHappyBag());
            record.setIsDel(ticket.getIsDel());
            return ticketMapper.updateByPrimaryKey(record)>0?"success":"fail";
        }
        return ticketMapper.updateByPrimaryKeySelective(record)>0?"success":"fail";
    }

    @Override
    public RspDataTableDto<Ticket> findAllTicketList(ReqDataTableDto dataTableDTO, String marketId,
                                                    String storeId,String typeId,String happyBag) {

        PageHelper.startPage(dataTableDTO.getPage()+1,dataTableDTO.getLength());
        Map<String,Object> map= new HashMap<String,Object>();
        map.put("marketId",StringUtils.isBlank(marketId)?null:Long.parseLong(marketId));
        map.put("storeId",StringUtils.isBlank(storeId)?null:Long.parseLong(storeId));
        map.put("typeId",StringUtils.isBlank(typeId)?null:Long.parseLong(typeId));
        map.put("happyBag",StringUtils.isBlank(happyBag)?null:Integer.parseInt(happyBag));
        List<Ticket> list = ticketMapper.findAllTicketList(map);
        PageInfo<Ticket> pageInfo =  new PageInfo<Ticket>(list);
        //统计优惠券的领取张数和使用张数
        List<Map<String,Object>> mapList = ticketMapper.findTicketStatics();
        for(Ticket ticket:pageInfo.getList()){
            boolean flag = true;
            for(Map<String,Object> ticketMap:mapList){
                if(ticket.getId().equals(Long.valueOf(String.valueOf(ticketMap.get("ticket_id"))))){
                    flag = false;
                    ticket.setTotalCount(Long.valueOf(String.valueOf(ticketMap.get("total_count"))));
                    ticket.setIsUseCount(Long.valueOf(String.valueOf(ticketMap.get("is_use_count"))));
                    break;
                }
            }
            if(flag){
                ticket.setTotalCount(new Long(0));
                ticket.setIsUseCount(new Long(0));
            }
        }
        return new RspDataTableDto<Ticket>(dataTableDTO.getDraw(),pageInfo.getTotal(),pageInfo.getList());

    }

    @Override
    public Map<String, Object> getCompelexList() {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("marketList",marketService.getMarketList());
        map.put("storeList",storeService.getStoreList());
        map.put("ticketTypeList",ticketTypeMapper.getTicketTypeList());
        return map;
    }

    @Override
    @Transactional(readOnly = false)
    public String saveTicket(Ticket ticket, String forever) {
        ticket.setIsForever(Integer.parseInt(forever)==1?true:false);
        return ticket.getId() != null ?updateByPrimaryKeySelective(ticket):insertSelective(ticket);
    }

    @Override
    public Ticket selectAllInfobyId(Long id) {
        return ticketMapper.selectAllInfobyId(id);
    }

    @Override
    @Transactional(readOnly = false)
    public void modifyBag(Long id) {
        Ticket ticket = selectByPrimaryKey(id);
        if(ticket.getIsHappyBag()){
            ticket.setIsHappyBag(false);
        }else{
            ticket.setIsHappyBag(true);
        }
        updateByPrimaryKeySelective(ticket);
    }
}
